<template>
  <div>
    <my-header></my-header>
    <div class="content">
      <!-- 2楼 -->
      <div class="genre" :style="{ height: hide ? '188px' : '302px' }">
        <!-- 第一层 -->
        <div class="difgenre">
          <div class="left">频道</div>
          <div class="right">
            <ul>
              <li
                :class="index == i ? 'current' : ''"
                v-for="(l, i) of genre"
                :key="i"
                @click="change(i)"
              >
                <router-link :to="{ path: 'ireader', query: { fid: l.fid } }">{{
                  l.fname
                }}</router-link>
              </li>
            </ul>
          </div>
        </div>
        <!-- 第二层 -->
        <div class="difgenre">
          <div class="left">类型</div>
          <div class="right">
            <ul :class="[hide ? '' : 'click']">
              <li
                :class="sub == i ? 'current' : ''"
                v-for="(item, i) of sort"
                :key="i"
                @click="classify(i)">
                <router-link :to="{ path: 'ireader', query: { lid: item.lid } }">{{ item.fname }}</router-link>
              </li>
            </ul>
            <span
              class="more"
              @click="show"
              v-show="sort.length > 12"
              :style="{ 'margin-top': hide ? '-33px' : '-160px' }"
            >
              <a href="javascript:;">
                <i>{{ hide ? "更多" : "收起" }}</i>
                <s :class="[hide ? '' : 'toggle']"></s>
              </a>
            </span>
          </div>
        </div>
        <!-- 第三层 -->
        <div class="difgenre">
          <div class="left">其他</div>
          <div class="right other">
            <ul>
              <li class="current">
                <a href="#">全部</a>
              </li>
              <li>
                <a href="#">免费</a>
              </li>
              <li>
                <a href="#">特价</a>
              </li>
              <li>
                <a href="#">vip</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
      <!-- 展示标题  开始 -->
      <div class="showTitle">
        <span class="hot current">
          <a
            href="javascript:;"
            >热门</a
          >
        </span>
        <span class="new">
          <a
            href="javascript:;"
            >最新</a
          >
        </span>
        <span class="comment">
          <a
            href="javascript:;"
            >好评</a
          >
        </span>
        <p>
          <a
            href="javascript:;"
            class="leftArro"
          >
            <s></s>
          </a>
          第<i>1</i>页
          <a
            href="javascript:;"
            class="rightArro"
          >
            <s></s>
          </a>
        </p>
      </div>
      <!-- 展示内容  开始 -->
      <div class="show">
        <ul class="newShow">
          <li v-for="(p, i) of arr" :key="i">
            <router-link :to="`/details/${p.bid}`" target="_blank">
              <img :src="`/${p.md_img}`" />
            </router-link>
            <div class="bookMation">
              <h3>
                <router-link :to="`/details/${p.bid}`" target="_blank">{{ p.title }}</router-link>
              </h3>
              <p class="tryread">
                {{ p.author }}
                <router-link :to="{path:'/read',query:{bid:p.bid,cid:0}}" target="_blank">试读</router-link>
              </p>
              <span> <s></s>{{ p.browse }}</span>
              <p class="introduce">{{ p.brief }}</p>
            </div>
          </li>
        </ul>
      </div>
      <!-- 翻页内容  开始 -->
      <div class="changepage">
        <span>
          <a
            href="javascript:;"
            class="up"
            @click="dec"
            :style="{ cursor: num == 1 ? 'not-allowed' : '' }"
            :class="num == 1 ? 'pro' : ''"
          >
            <s></s>
            <i>上一页</i>
          </a>
          <a
            href="javascript:;"
            v-for="item in pageNum"
            :key="item"
            :class="num == item ? 'cur' : ''"
            @click="cur(item)"
            >{{ item }}</a
          >
          <a class="noBg">...</a>
          <a
            href="javascript:;"
            class="down"
            @click="add"
            :style="{ cursor: num  == pageNum ? 'not-allowed' : '' }"
            :class="num  == pageNum ? 'pro' : ''"
          >
            <i>下一页</i>
            <s></s>
          </a>
        </span>
      </div>
    </div>
    <my-footer></my-footer>
    <my-top></my-top>
  </div>
</template>
<script>
import MyHeader from "../components/MyHeader.vue";
import MyFooter from "../components/MyFooter.vue";
import MyTop from "../components/MyTop.vue";
export default {
  components: {
    MyHeader,
    MyFooter,
    MyTop,
  },
  data() {
    return {
      genre: [],
      arr: [],
      index: 0,
      sort: [],
      sub: 0,
      hide: true,
      more: "更多",
      num: 1,
      pageNum: 1,
    };
  },
  methods: {
    change(i) {
      this.index = i;
      this.axios.get(`/ireader/${this.index + 1}`).then((result) => {
        console.log(result.data);
        this.sort = result.data;  
      });
      this.sub = 0;
      if(this.index==0){
        this.axios.get(`/ireader/page?lid=1&pno=${this.num}`)
        .then((result) => {
          this.arr = result.data;
        });
      }else{
         let lid = this.$route.query.lid;
         this.axios.get(`/ireader/page?lid=${lid}&pno=${this.num}`)
        .then((result) => {
          this.arr = result.data;
        });
      }
    },
    classify(i) {
      this.sub = i;
      let lid = this.$route.query.lid || 1;
      //点击二级分类，动态获取数据
      this.axios
        .get(`/ireader/page?lid=${lid}&pno=${this.num}`)
        .then((result) => {
          console.log(result.data)
          this.arr = result.data;
        });
    },
    show() {
      this.hide = !this.hide;
    },
    page(i) {
      this.n = i;
    },
    cur(item) {
      this.num = item;
      var lid = this.$route.query.lid || 1;
      //点击页码 动态获取数据
      this.axios
        .get(`/ireader/page?lid=${lid}&pno=${this.num}`)
        .then((result) => {
          this.arr = result.data;
        });
    },
    add() {
      if (this.num < this.pageNum) {
        this.num++;
      }
    },
    dec() {
      if (this.num > 1) {
        this.num--;
      }
    },
  },
  mounted() {
    this.cur(this.num);
    this.classify(this.sub);
    //请求一级分类
    this.axios.get("/ireader/fid").then((result) => {    
      this.genre = result.data;
    });
    this.change(this.index);
    let lid = this.$route.query.lid || 1;
    this.axios.get(`/ireader/count?lid=${lid}`).then((result) => {
      this.pageNum = Math.ceil(result.data.length / 60); //用数组向上取整动态获取每个分类请求回来的页码
    });
  },
};
</script>
<style scoped>
a {
  text-decoration: none;
  color: #9e9e9e;
}

ul {
  list-style: none;
}

h3,
i {
  font-style: normal;
  font-weight: normal;
  font-size: 12px;
}

input,
button {
  outline: none;
}

.content {
  width: 1000px;
  margin: 0 auto;
  padding-top: 40px;
}
.content .genre {
  height: 184px;
  padding: 0 46px;
  background-color: #fff;
}
.content .genre .difgenre {
  width: 1000px;
  height: 51px;
  padding: 20px 46px;
  font-size: 12px;
}
.content .genre .difgenre .left {
  width: 48px;
  height: 51px;
  float: left;
  line-height: 36px;
  color: #ccc;
}
.content .genre .difgenre .right {
  width: 848px;
  float: left;
  padding: 0 0 0 6px;
  border-bottom: 1px solid #f0f0f0;
  color: #9e9e9e;
}
.content .genre .difgenre .other {
  border-bottom: 0;
}
.content .genre .difgenre .right ul {
  width: 790px;
  height: 38px;
  line-height: 38px;
  overflow: hidden;
}
.content .genre .difgenre .right .click {
  width: 790px;
  height: 165px;
  background-color: #fff;
}
.content .genre .difgenre .right ul li {
  float: left;
  margin-right: 16px;
}
.content .genre .difgenre .right ul li a {
  display: inline-block;
  height: 20px;
  padding: 0 9px;
}
.content .genre .difgenre .right ul .current a {
  line-height: 18px;
  border: 1px solid #9e9e9e;
  border-radius: 50px;
  background-color: #9e9e9e;
  color: #fff;
}
.content .genre .difgenre .right .more {
  float: right;
  margin-top: -33px;
}
.content .genre .difgenre .right .more a {
  display: inline-block;
  width: 55px;
  height: 20px;
  line-height: 20px;
  padding: 0 9px;
  margin-left: -5px;
}
.content .genre .difgenre .right .more a i {
  float: left;
}
.content .genre .difgenre .right .more a s {
  float: right;
  width: 9px;
  height: 6px;
  margin-top: 7px;
  background-image: url(/img/icon/listIcon.png);
  background-position: 0 0;
}
.content .genre .difgenre .right .more a .toggle {
  background-image: url(/img/icon/listIcon.png);
  background-position: -9px 0;
}
.content .showTitle {
  width: 1000px;
  height: 41px;
  margin-top: 23px;
}
.content .showTitle .current {
  background-color: #fff;
  height: 41px;
}
.content .showTitle .new {
  margin: 0 1px;
}
.content .showTitle span {
  float: left;
  width: 114px;
  line-height: 40px;
  text-align: center;
  background-color: #f5f5f5;
  font-size: 14px;
  border-radius: 3px;
}
.content .showTitle p {
  width: 144px;
  height: 34px;
  line-height: 34px;
  text-align: center;
  float: right;
  letter-spacing: 1px;
  font-size: 12px;
  color: #9e9e9e;
}
.content .showTitle p a {
  float: left;
  display: inline-block;
  width: 32px;
  height: 34px;
  line-height: 36px;
  text-align: center;
  border: 1px solid #f0f0f0;
  border-radius: 3px;
  background-color: #fff;
}
.content .showTitle p a s {
  background-image: url(/img/icon/listIcon.png);
  display: inline-block;
  width: 7px;
  height: 11px;
}
.content .showTitle p .leftArro s {
  background-position: 0 -38px;
}
.content .showTitle p .rightArro {
  float: right;
}
.content .showTitle p .rightArro s {
  background-position: 0 -50px;
}
.content .showTitle p i {
  margin: 0 2px;
  font-style: normal;
}
.content .show {
  display: flex;
  width: 1000px;
  background-color: #fff;
  padding: 42px 35px 0;
}
.content .show li {
  width: 309px;
  height: 142px;
  padding: 0 20px;
  margin-bottom: 44px;
  float: left;
}
.content .show li img {
  float: left;
  width: 107px;
  height: 142px;
  border: 1px solid #f0f0f0;
  margin-right: 14px;
}
.content .show li .bookMation {
  float: left;
  width: 148px;
  height: 142px;
}
.content .show li .bookMation h3 {
  font-style: normal;
  font-weight: normal;
  font-size: 14px;
  border-bottom: 1px solid #f0f0f0;
  width: 148px;
  height: 27px;
  margin-bottom: 10px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.content .show li .bookMation h3 a {
  color: #212121;
}
.content .show li .bookMation .tryread {
  width: 148px;
  height: 20px;
  font-size: 12px;
  color: #9e9e9e;
}
.content .show li .bookMation .tryread a {
  width: 34px;
  height: 22px;
  line-height: 22px;
  text-align: center;
  color: #fff;
  border-radius: 3px;
  float: right;
  background-color: #f26552;
}
.content .show li .bookMation span {
  font-size: 12px;
  color: #9e9e9e;
}
.content .show li .bookMation span s {
  background-image: url(/img/icon/icon.png);
  background-position: -86px -73px;
  margin: 9px 6px 14px 0;
  width: 14px;
  height: 9px;
  float: left;
}
.content .show li .bookMation .introduce {
  width: 148px;
  height: 48px;
  line-height: 16px;
  font-size: 12px;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  color: #9e9e9e;
  margin-top: 8px;
}
.content .changepage {
  width: 1000px;
  height: 58px;
  padding-top: 22px;
  text-align: center;
}
.content .changepage a {
  width: 35px;
  height: 35px;
  display: inline-block;
  background-color: #fff;
  line-height: 35px;
  text-align: center;
  border-radius: 3px;
  border: 1px solid #fff;
  margin: 0 8px;
}
.content .changepage a:hover {
  border: 1px solid #9a9a9a;
}
.content .changepage .noBg {
  background: none;
  border: none;
  line-height: 30px;
}
.content .changepage .noBg:hover {
  border: none;
}
.content .changepage .cur {
  background: #e0e0e0;
  border: 1px solid #e0e0e0;
  color: #fff;
}
.content .changepage .cur:hover {
  border: none;
}
.content .changepage .up,
.content .changepage .down {
  display: inline-block;
  width: 82px;
  height: 36px;
  background-color: #fff;
}
.content .changepage .pro:hover {
  border-color: transparent;
}
.content .changepage .up i,
.content .changepage .down i {
  line-height: 36px;
}
.content .changepage .up {
  text-align: left;
}
.content .changepage .up s {
  width: 7px;
  height: 11px;
  margin: 12px 8px 0 14px;
  float: left;
  background-image: url(/img/icon/listIcon.png);
  background-position: -9px -9px;
}
.content .changepage .down {
  text-align: right;
}
.content .changepage .down s {
  width: 7px;
  height: 11px;
  margin: 12px 8px 0 14px;
  float: right;
  background-image: url(/img/icon/listIcon.png);
  background-position: 0 -50px;
  margin-left: 8px;
}
</style>
